using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Universal.Api.Attributes;
using Universal.Application.Contracts.Dto.ArticleCategory;
using Universal.Application.Repositories;

namespace Universal.Api.Controllers;

[Authorize]
[ApiController]
[Route("api/[controller]")]
public class ArticleCategoryController : ControllerBase
{
    private readonly IArticleCategoryService _articleCategoryService;

    public ArticleCategoryController(IArticleCategoryService articleCategoryService)
    {
        _articleCategoryService = articleCategoryService;
    }

    /// <summary>
    /// 创建文章分类
    /// </summary>
    [HttpPost]
    [RequirePermission("category.Create")]
    public async Task<IActionResult> CreateAsync([FromBody] CreateArticleCategoryDto dto)
    {
        var result = await _articleCategoryService.CreateAsync(dto);
        return Ok(result);
    }

    /// <summary>
    /// 更新文章分类
    /// </summary>
    [HttpPut]
    [RequirePermission("category.Edit")]
    public async Task<IActionResult> UpdateAsync([FromBody] UpdateArticleCategoryDto dto)
    {
        var result = await _articleCategoryService.UpdateAsync(dto);
        return Ok(result);
    }

    /// <summary>
    /// 删除文章分类
    /// </summary>
    [HttpDelete("{id}")]
    [RequirePermission("category.Delete")]
    public async Task<IActionResult> DeleteAsync([FromRoute] Guid id)
    {
        var result = await _articleCategoryService.DeleteAsync(id);
        return Ok(result);
    }

    /// <summary>
    /// 根据ID获取文章分类
    /// </summary>
    [HttpGet("{id}")]
    public async Task<IActionResult> GetByIdAsync([FromRoute] Guid id)
    {
        var result = await _articleCategoryService.GetByIdAsync(id);
        return Ok(result);
    }

    /// <summary>
    /// 获取文章分类树形结构
    /// </summary>
    [HttpGet("tree")]
    public async Task<IActionResult> GetTreeAsync([FromQuery] bool activeOnly = true)
    {
        var result = await _articleCategoryService.GetTreeAsync(activeOnly);
        return Ok(result);
    }

    /// <summary>
    /// 分页查询文章分类
    /// </summary>
    [HttpGet("paged")]
    public async Task<IActionResult> GetPagedAsync([FromQuery] ArticleCategoryQueryDto query)
    {
        var result = await _articleCategoryService.GetPagedAsync(query);
        return Ok(result);
    }

    /// <summary>
    /// 获取子分类列表
    /// </summary>
    [HttpGet("children")]
    public async Task<IActionResult> GetChildrenAsync([FromQuery] Guid? parentId)
    {
        var result = await _articleCategoryService.GetChildrenAsync(parentId);
        return Ok(result);
    }

    /// <summary>
    /// 切换激活状态
    /// </summary>
    [HttpPut("{id}/toggle-active")]
    [RequirePermission("category.Edit")]
    public async Task<IActionResult> ToggleActiveAsync([FromRoute] Guid id)
    {
        var result = await _articleCategoryService.ToggleActiveAsync(id);
        return Ok(result);
    }

    /// <summary>
    /// 检查分类名称是否唯一
    /// </summary>
    [HttpGet("check-name")]
    public async Task<IActionResult> IsNameUniqueAsync([FromQuery] string name, [FromQuery] Guid? parentId, [FromQuery] Guid? excludeId)
    {
        var result = await _articleCategoryService.IsNameUniqueAsync(name, parentId, excludeId);
        return Ok(result);
    }

    /// <summary>
    /// 分配子分类
    /// </summary>
    [HttpPost("assign-children")]
    [RequirePermission("category.Edit")]
    public async Task<IActionResult> AssignChildrenAsync([FromBody] AssignChildrenDto assignChildrenDto)
    {
        var result = await _articleCategoryService.AssignChildrenAsync(assignChildrenDto);
        return Ok(result);
    }

    /// <summary>
    /// 移除子分类
    /// </summary>
    [HttpPost("remove-children")]
    [RequirePermission("category.Edit")]
    public async Task<IActionResult> RemoveChildrenAsync([FromBody] AssignChildrenDto assignChildrenDto)
    {
        var result = await _articleCategoryService.RemoveChildrenAsync(assignChildrenDto);
        return Ok(result);
    }

    /// <summary>
    /// 分配文章到分类
    /// </summary>
    [HttpPost("assign-articles")]
    [RequirePermission("category.Edit")]
    public async Task<IActionResult> AssignArticlesAsync([FromBody] AssignArticlesDto assignArticlesDto)
    {
        var result = await _articleCategoryService.AssignArticlesAsync(assignArticlesDto);
        return Ok(result);
    }

    /// <summary>
    /// 从分类移除文章
    /// </summary>
    [HttpPost("remove-articles")]
    [RequirePermission("category.Edit")]
    public async Task<IActionResult> RemoveArticlesAsync([FromBody] AssignArticlesDto assignArticlesDto)
    {
        var result = await _articleCategoryService.RemoveArticlesAsync(assignArticlesDto);
        return Ok(result);
    }
}
